//
//  ProgressView.m
//  Village
//
//  Created by 程虎 on 2017/10/10.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "ProgressView.h"

@interface ProgressView ()
@property (nonatomic, strong) UILabel *proLab;
@property (nonatomic, strong) UIProgressView *progressView;
@end

@implementation ProgressView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
        self.backgroundColor = [Util colorWithHexString:@"#212121" alpha:0.5];
        [self set_UI];
    }
    return self;
}

- (void)setProssCount:(CGFloat)prossCount
{
    _prossCount = prossCount;
    LSLog(@"进度条 ----  %f",_prossCount);
    self.progressView.progress = _prossCount;
    self.proLab.text = [NSString stringWithFormat:@"%0.f%%",_prossCount * 100];
}

- (void)set_UI
{
    UIView *bkView = [UIView new];
    bkView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgs = [UIImageView new];
    imgs.image = [UIImage imageNamed:@"logo"];
    
    UILabel *lab = [[Tools shareInstance] creat_Label:@"正在下载" Color:K_TitlesColor Font:22 tag:-1 Alignment:0 Lines:1];
    
    self.progressView = [[UIProgressView alloc]init];
    
    //设置它的风格，为默认的
    _progressView.progressViewStyle = UIProgressViewStyleDefault;
    
    //设置轨道的颜色
    _progressView.trackTintColor = [Util colorWithHexString:@"#dbdbdb"];
    
    //设置进度的颜色
    _progressView.progressTintColor = KRedColors;
    
    //设置进度的初始值，即初始位置。范围是0.0-1.0
    _progressView.progress = 0.0;
    
    self.proLab = [[Tools shareInstance] creat_Label:@"0%" Color:[Util colorWithHexString:@"#666666"] Font:13 tag:-1 Alignment:2 Lines:1];
    
    UIButton *btn = [[Tools shareInstance] creat_Button:@"取消" Color:KRedColors BackColors:[UIColor whiteColor] Font:19 tag:10];
    [btn addTarget:self action:@selector(btnClickBlock) forControlEvents:UIControlEventTouchUpInside];
    
    [bkView sd_addSubviews:@[imgs,lab,self.progressView,self.proLab,btn]];
    [self addSubview:bkView];
   
    CGFloat left = 20 * AutoScaleX;
    
    bkView.sd_layout
    .leftSpaceToView(self, left)
    .rightSpaceToView(self, left)
    .centerXEqualToView(self)
    .centerYEqualToView(self);
    
    imgs.sd_layout
    .leftSpaceToView(bkView, 20)
    .topSpaceToView(bkView, 20)
    .widthIs(30)
    .heightEqualToWidth();
    
    lab.sd_layout
    .leftSpaceToView(imgs, 10)
    .centerYEqualToView(imgs)
    .rightSpaceToView(bkView, 20)
    .heightIs(25);
    
    self.progressView.sd_layout
    .leftEqualToView(imgs)
    .topSpaceToView(imgs, 20)
    .rightEqualToView(lab)
    .heightIs(9);
    
    self.proLab.sd_layout
    .rightEqualToView(self.progressView)
    .topSpaceToView(self.progressView, 10)
    .widthIs(100)
    .heightIs(25);
    
    btn.sd_layout
    .rightEqualToView(self.proLab)
    .topSpaceToView(self.proLab, 20)
    .widthIs(50)
    .heightIs(45);
    
    [bkView setupAutoHeightWithBottomView:btn bottomMargin:15];
}

- (void)btnClickBlock
{
    [self hide];
}

- (void)hide
{
    [self removeFromSuperview];
}


- (void)show
{
    UIView *view = nil;
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        if (!window.isKeyWindow)continue;
        view = window;
        break;
    }
    [view addSubview:self];
}
@end
